package com.ruoyi.internet.controller;

import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.internet.domain.IntMaterial;
import com.ruoyi.internet.service.IIntMaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 原材料采购Controller
 *
 * @author cjie
 * @date 2021-08-16
 */
@Api(tags = " 原材料采购")
@RestController
@RequestMapping("/intMaterial")
public class IntMaterialController extends BaseController {
    @Autowired
    private IIntMaterialService intMaterialService;

    /**
     * 查询原材料采购列表
     */
    @ApiOperation(value="查询原材料采购列表", notes="查询原材料采购列表")
    @GetMapping("/list")
    public TableDataInfo list(IntMaterial intMaterial) {
        startPage();
        List<IntMaterial> list = intMaterialService.selectIntMaterialList(intMaterial);
        //未登录，去掉联系人和电话
        if(StringUtils.isEmpty(SecurityUtils.getUsername())){
            list.forEach(intMaterial1 -> {
                intMaterial1.setLinkMan(null);
                intMaterial1.setPhone(null);
            });
        }
        return getDataTable(list);
    }

    /**
     * 导出原材料采购列表
     */
    @ApiOperation(value="导出原材料采购列表", notes="导出原材料采购列表")
    @Log(title = "原材料采购", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IntMaterial intMaterial) throws IOException {
        List<IntMaterial> list = intMaterialService.selectIntMaterialList(intMaterial);
        ExcelUtil<IntMaterial> util = new ExcelUtil<IntMaterial>(IntMaterial.class);
        util.exportExcel(response, list, "原材料采购数据");
    }
    /**
     * 获取原材料采购详细信息
     */
    @ApiOperation(value="获取原材料采购详细信息", notes="获取原材料采购详细信息")
    @GetMapping(value = "/info/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(intMaterialService.selectIntMaterialById(id));
    }

    /**
     * 新增原材料采购
     */
    @ApiOperation(value="新增原材料采购", notes="新增原材料采购")
    @PreAuthorize(hasPublish = "Y")
    @Log(title = "原材料采购", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IntMaterial intMaterial) {
        return toAjax(intMaterialService.insertIntMaterial(intMaterial));
    }

    /**
     * 修改原材料采购
     */
    @ApiOperation(value="修改原材料采购", notes="修改原材料采购")
    @PreAuthorize(hasPublish = "Y")
    @Log(title = "原材料采购", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IntMaterial intMaterial) {
        return toAjax(intMaterialService.updateIntMaterial(intMaterial));
    }

    /**
     * 删除原材料采购
     */
    @ApiOperation(value="删除原材料采购", notes="删除原材料采购")
    @ApiImplicitParam(name = "ids", value = "原材料采购ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPublish = "Y")
    @Log(title = "原材料采购", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(intMaterialService.deleteIntMaterialByIds(ids));
    }
}
